package com.project.until;

import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.net.URL;
import java.net.URLDecoder;

public class UploadUtil {
    public static String upload(MultipartFile mf, String dirPath) {
        //得到上传文件名
        String fileName = mf.getOriginalFilename();
        //讲文件名以时间毫秒数重命名
        fileName = System.currentTimeMillis() + fileName.substring(fileName.lastIndexOf("."));
        try {
            //得到上传文件存放目录的真实目录
            URL url = Thread.currentThread().getContextClassLoader()
                    .getResources(dirPath).nextElement();
            String filePath = URLDecoder.decode(url.getFile(), "utf-8");
            //将文件比导弹中的二进制数组保存进指定文件
            mf.transferTo(new File(filePath + "/" + fileName));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return fileName;
    }
}
